﻿using FS.Common.DotNetBean;
using FS.Common.DotNetCode;
using FS.Common.DotNetUI;
using FS.Web;
using FS.Business;
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;

namespace FS.Web.BLL.Visa_Base
{
    public partial class Visa_infoForm : PageBase
    {
        private string _key;
        private string _issaveadd;
        visa_dao _visa = new visa_dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            this._key = base.Request["key"];
            this._issaveadd = base.Request["saveadd"];
            string ls_viewSupply = RequestSession.GetSessionUser().isViewSupply;
            string ls_viewPrice = RequestSession.GetSessionUser().isViewPrice;
            string ls_viewCost = RequestSession.GetSessionUser().isViewCost;

            this.HD_viewSupply.Value = ls_viewSupply;

            if (!base.IsPostBack)
            {
                if (this._issaveadd=="N")
                { 
                    this.SaveAdd.Visible = false; 
                }

                this.GetSelect_TrustInfo();
                this.GetSelect_InfoFlag();                
                this.initBase_City();
                initBase_postby();
                initBase_moneytype();
                initBase_moneyflag();

                #region 权限控制               
                if (ls_viewSupply == "0")
                {
                    this.div_Supply1.Visible = false;
                    this.div_Supply2.Visible = false;
                }
                if (ls_viewPrice == "0")
                {
                    this.div_Price1.Visible = false;
                    this.div_Price2.Visible = false;
                }
                if (ls_viewCost == "0")
                {
                    
                }
                #endregion               

                //设置签证时间
                this.info_getdate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                                
                if (string.IsNullOrEmpty(this._key))
                {
                    this.Hinfo.Value = CommonHelper.GetGuid;
                    this.trust_id.SelectedIndex = 0;
                    this.detail_type.SelectedIndex = 0;
                }
                else
                {
                    this.Hinfo.Value = this._key;                   
                    this.InitData();
                    DataBindGrid();

                    //编辑权限
                    this.img_trust.Visible = false;
                    this.img_supply.Visible = false;
                }
            }
        }

        #region 初始项目数据绑定
        //委托方
        private void GetSelect_TrustInfo()
        {
            DataTable dt = _visa.GetSelect_TrustInfo();
            ControlBindHelper.BindHtmlSelect(dt, this.trust_id, "name", "trust_id", "请选择");
        }

        //签证状态
        private void GetSelect_InfoFlag()
        {
            DataTable dt = _visa.GetSelect_InfoFlag();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_infoflag, "property_value", "property_key", "");
        }

        //取件类型
        private void initBase_postby()
        {
            DataTable dt = _visa.GetSelect_postby();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_postby, "property_value", "property_key", "");
        }

        //收款方式
        private void initBase_moneytype()
        {
            DataTable dt = _visa.GetSelect_moneytype();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_moneytype, "property_value", "property_key", "");
        }

        //收款情况
        private void initBase_moneyflag()
        {
            DataTable dt = _visa.GetSelect_moneyflag();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_moneyflag, "property_value", "property_key", "");
        }

        #endregion

        #region 明细下拉
        
        //所属国家
        private void initBase_City()
        {
            DataTable dt = _visa.GetSelect_CityInfo();
            ControlBindHelper.BindHtmlSelect(dt, this.detail_city, "city_name", "city_id", "请选择");
        }       
       
        #endregion

        private void InitData()
        {
            Hashtable ht = DataFactory.Run().GetHashtableById("Visa_Info", "Info_ID", this._key);
            if (ht.Count > 0 && ht != null)
            {
                ht["INFO_GETDATE"] = (Convert.ToDateTime(ht["INFO_GETDATE"])).ToString("yyyy-MM-dd");
               
                string ls_trust = ht["TRUST_ID"].ToString();
                DataTable dt = _visa.GetSelect_trustInfoBytrustid(ls_trust);
                if (dt.Rows.Count > 0)
                {
                    ht["Ref_pricetype"] = dt.Rows[0]["type_price"].ToString();
                    ht["Ref_trusttype"] = dt.Rows[0]["type_trust"].ToString(); 
                    this.Hpricecode.Value=dt.Rows[0]["type_trust"].ToString();                       
                }
                ControlBindHelper.SetWebControls(this.Page, ht);
            }
        }

        private int FormSave()
        {
            Hashtable ht = new Hashtable();
            //ht = ControlBindHelper.GetWebControls(this.Page);
            string ls_infoid = this.Hinfo.Value.Trim();
            string ls_people = this.info_outpeople.Value;
            if (ls_people == "0")
            {
                ShowMsgHelper.Alert_Error("请填写客户信息！");
                return 0;
            }

            ht["trust_id"] = this.trust_id.Value;
            ht["info_operation"] = this.info_operation.Value;
            ht["info_getdate"] = this.info_getdate.Value;
            ht["Ref_infoflag"] = "Raise";
            ht["info_remark"] = this.info_remark.Value;
            
            ht["info_outpeople"] = this.info_outpeople.Value;
            ht["info_outmoney"] = this.info_outmoney.Value;
            ht["info_moneyin"] = this.info_moneyin.Value;
            ht["info_moneyout"]=this.info_moneyout.Value;
            ht["Ref_moneyflag"]=this.Ref_moneyflag.Value;
            ht["Ref_moneytype"]=this.Ref_moneytype.Value;
            ht["Ref_postmoney"]=this.Ref_postmoney.Value;
            ht["Ref_postby"]=this.Ref_postby.Value;
            ht["Info_fapiao"]=this.Info_fapiao.Value;
            ht["info_moneyfapiao"]=this.info_moneyfapiao.Value;
            ht["info_TuanNo"]=this.info_TuanNo.Value;
            ht["info_contactnumber"]=this.info_contactnumber.Value;
            ht["info_moneyfapiao"]=this.info_moneyfapiao.Value;

            string ls_updateid = RequestSession.GetSessionUser().UserId.ToString();
            string ls_updatename = RequestSession.GetSessionUser().UserName.ToString();
            string ls_AddNo = this.HD_AddNo.Value.Trim();
            bool IsOk = false;
            if (ls_AddNo == "0")
            {
                ht["Info_ID"] = ls_infoid;
                ht["CreateUserId"] = ls_updateid;
                ht["CreateUserName"] = ls_updatename;
                IsOk = DataFactory.Run().Submit_AddOrEdit("Visa_Info", "Info_ID", this._key, ht);
            }
            else
            {
                ht["ModifyDate"] = DateTime.Now;
                ht["ModifyUserId"] = ls_updateid;
                ht["ModifyUserName"] = ls_updatename;
                IsOk = DataFactory.Run().Submit_AddOrEdit("Visa_Info", "Info_ID", ls_infoid, ht);
            }
            if (IsOk)
            {
                if (ls_AddNo == "0")
                {
                    //计算团号
                    this.HD_AddNo.Value = "1";
                    _visa.UpdateInfoNo(ls_infoid);
                }
                return 1;
            }
            return -1;
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            int rtn = FormSave();
            if (rtn > 0)
            {
                ShowMsgHelper.Alert("操作成功！");
            }
            else
            {
                ShowMsgHelper.Alert_Error("操作失败！");
            }
        }
               
        protected void lbtadd_Click(object sender, EventArgs e)
        {
            this.DataBindGrid();
            this.detail_customer.Focus();
            
        }

        protected void lbtdel_Click(object sender, EventArgs e)
        {
            string ls_detailid = this.HDetailID.Value;
            if (!string.IsNullOrEmpty(ls_detailid))
            {
                _visa.DeleteInfoDetail(ls_detailid);
                this.DataBindGrid();
            }
        }  
       
        private void DataBindGrid()
        {
            string ls_infotid = this.Hinfo.Value.Trim();
            DataTable dt = _visa.GetInfoDetail(ls_infotid);
            DataTable dtsum = _visa.GetTotalInfoDetail(ls_infotid);
            ControlBindHelper.BindRepeaterList(dt, this.rp_Item);
            string ls_people = "0";
            string ls_money = "0";
            string ls_cost = "0";
            string ls_post = this.Ref_postmoney.Value.ToString();
            if (ls_post == "") {
                ls_post = "0";
            }

            if (dtsum.Rows.Count > 0)            
            {
                ls_people = dtsum.Rows[0]["totalpeo"].ToString();
                ls_money= dtsum.Rows[0]["price"].ToString();
                ls_cost = dtsum.Rows[0]["cost"].ToString();
            }
            this.info_outpeople.Value = ls_people;
            this.info_outmoney.Value = ls_money;
            this.info_moneyout.Value = ls_cost;
            this.info_moneyin.Value = Convert.ToString(Convert.ToDecimal(ls_money) + Convert.ToDecimal(ls_post));  
        }

        protected void SaveAdd_Click(object sender, EventArgs e)
        {
            int rtn = FormSave();
            if (rtn > 0)
            {
                ShowMsgHelper.AlertMsg("操作成功！");
            }
            else
            {
                ShowMsgHelper.Alert_Error("操作失败！");
            }
        }

        
    }
}